<template>
    <input-files
            :data-file="dataFile"
            :file-disk="fileDisk"
            :file-upload-timely="true"
            :multiple="false"
            :disabled="disabled"
            :data-button-text="dataButtonText"
            :select-file-callback="selectFileCallback"
            v-on:change="handleChange"
            v-on:upload-success="handleUploadSuccess"
    >

    </input-files>

</template>

<script>

  export default {
    components: {},
    props: {
      fileDisk: {
        type: String,
        default: ''
      },
      dataPassword: {
        type: String,
        default: ''
      },
      dataFile: {
        type: [Object, String],
        default: () => {
          return {
            url: '',
            url_thumb: '',
            path: ''
          }
        }
      },
      dataButtonText: {
        type: String,
        default: '加密上传'
      },
      disabled: {
        type: Boolean,
        default: false
      },

    },
    data() {
      return {
        downUrl:'',
        blobFile:null,
      }
    },
    created() {
      let _this = this;
      if (typeof(QCefClient) != 'undefined') {
        QCefClient.addEventListener("PackEndEvent", function (event) {
          //alert(LeUtils.valueGet(event,'color',''));
          _this.downUrl = LeUtils.valueGet(event,'url','')
        })
      }


    },
    mounted() {

    },
    watch: {

    },
    methods: {
      handleChange:function (input,source) {
        this.$emit('change', input,source)
      },
      handleUploadSuccess:function (source) {
        _this.blobFile=null;
        this.$emit('upload-success', source)
      },
      selectFileCallback: function () {
        let _this = this;
        _this.downUrl = '';
        //_this.downUrl = 'http://127.0.0.1:53111/temp1597392419.zip';
        if (typeof(QCefClient) == 'undefined') {
          alert('此浏览器不支持文件加密')
          return false;
        }

        QCefClient.invokeMethod("uploadFile",_this.dataPassword);

        return new Promise(function (resolve, reject) {
          LeUtils.timeCountDown(3600, function () {
            _this.$message.error('加密文件失败');
          },function () {
            if(_this.downUrl){
              //console.log('222')
              let fileDownUrl = _this.downUrl;
              _this.downUrl = '';
              fetch(fileDownUrl).then(res => res.blob().then(blob => {
                _this.blobFile =  new File([blob], LeUtils.baseName(fileDownUrl) , {type: "application/zip", lastModified: new Date()});
                resolve(_this.blobFile);
                QCefClient.invokeMethod("closeWait");
              }));
              return  true;
            }
          },1);

        })
      },
    }
  }
</script>
